package com.bio.jsf;

import com.bio.jpa.entities.*;
import com.bio.jsf.util.JsfUtil;
import com.bio.jpa.session.FeatureKeyFacade;
import java.awt.event.ActionEvent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.event.ValueChangeEvent;
import javax.swing.JOptionPane;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

@ManagedBean(name = "featureKeyController")
@SessionScoped
public class FeatureKeyController implements Serializable {

    private FeatureKey current;
    private Qualifier qualifier = new Qualifier();
    private List<FeatureKey> items = null;
    @EJB
    private com.bio.jpa.session.FeatureKeyFacade ejbFacade;

    public FeatureKeyController() {
    }

    public Qualifier getQualifier() {
        if (qualifier == null) {
            qualifier = new Qualifier();
        }
        return qualifier;
    }

    public void setQualifier(Qualifier qualifier) {
        this.qualifier = qualifier;
    }

    public FeatureKey findLast() {
        current = getFacade().edit(current);
        return current;
    }

    public FeatureKey getCurrent() {
        return current;
    }

    public void setCurrent(FeatureKey current) {
        this.current = current;
    }

    public void changeStatus(ValueChangeEvent valueChangeEvent) {
        JOptionPane.showMessageDialog(null, valueChangeEvent);
    }

    private FeatureKeyFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        recreateModel();
        return "List";
    }

    public String prepareView() {
        return "View";
    }

    public String prepareCreate() {
        current = new FeatureKey();
        return "Create";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("FeatureKeyCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("FeatureKeyUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public void update(FeatureKey fk) {
        try {
            getFacade().edit(fk);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("FeatureKeyUpdated"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public void addQualifier(ActionEvent act, FeatureKey fk, Qualifier q) {
        RequestContext context = RequestContext.getCurrentInstance();
    }

    public String destroy() {
        performDestroy();
        return "List";
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("FeatureKeyDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public List<FeatureKey> getItems() {
        items = getFacade().findAll();
        return items;
    }

    public List<FeatureKey> loadItems(Feature feat) {
        items = getFacade().findItems(feat.getIdFeature());
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    @FacesConverter(forClass = FeatureKey.class)
    public static class FeatureKeyControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            FeatureKeyController controller = (FeatureKeyController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "featureKeyController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof FeatureKey) {
                FeatureKey o = (FeatureKey) object;
                return getStringKey(o.getIdFeatureKey());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + FeatureKeyController.class.getName());
            }
        }
    }
}
